/*
 * 题目描述：给出所有图书的信息，再给出某个字段进行查找
 *
 * 输入：简单，略
 *
 */
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
struct Book{
	string id;
	string title;
	string author;
	vector<string> keywords;
	string publisher;
	string year;
	Book(string id, string title, string author,string keywords,string publisher, string year){
		this->id = id;
		this->title = title;
		this->author = author;
		this->publisher = publisher;
		this->year = year;
		istringstream iss(keywords);
		do{
			string word;
			iss >> word;
			if(word.empty()){
				break;
			}
			this->keywords.push_back(word);
		}while(iss);
	}
};

int N,M;
vector<Book> books;

vector<Book> findByTitle(string title){
	vector<Book> res;
	for(int i=0; i<books.size(); i++){
		if(books[i].title == title){
			res.push_back(books[i]);
		}
	}
	return res;
}

vector<Book> findByAuthor(string author){
	vector<Book> res;
	for(int i=0; i<books.size(); i++){
		if(books[i].author == author){
			res.push_back(books[i]);
		}
	}
	return res;
}

vector<Book> findByKeyword(string keyword){
	vector<Book> res;
	for(int i=0; i<books.size(); i++){
		if(find(books[i].keywords.begin(),books[i].keywords.end(),keyword)!=books[i].keywords.end()){
			res.push_back(books[i]);
		}
	}
	return res;
}

bool cmp(Book &b1, Book &b2){
	int num1 = 0;
	int num2 = 0;
	for(int i=0; i<b1.id.length();i++){
		num1 = num1 * 10 + b1.id[i] - '0';
	}
	for(int i=0; i<b2.id.length();i++){
		num2 = num2 * 10 + b2.id[i] - '0';
	}
	return num1 < num2;
}

vector<Book> findByPublisher(string publisher){
	vector<Book> res;
	for(int i=0; i<books.size(); i++){
		if(books[i].publisher == publisher){
			res.push_back(books[i]);
		}
	}
	return res;
}

vector<Book> findByYear(string year){
	vector<Book> res;
	for(int i=0; i<books.size(); i++){
		if(books[i].year == year){
			res.push_back(books[i]);
		}
	}
	return res;
}

int main(){
	cin >> N;
	getchar();
	string id,title,author,keywords,publisher,year;
	while(N--){
		getline(cin,id);
		getline(cin,title);
		getline(cin,author);
		getline(cin,keywords);
		getline(cin,publisher);
		getline(cin,year);
		Book book(id,title,author,keywords,publisher,year);
		books.push_back(book);
	}
	cin >> M;
	getchar();
	string str,detail,cmd;
	vector<Book> res;
	while(M--){
		getline(cin,str);
		cout << str << endl;
		cmd = str.substr(0,1);
		detail = str.substr(3);
		if(cmd == "1"){
			res = findByTitle(detail);
		}else if(cmd == "2"){
			res = findByAuthor(detail);
		}else if(cmd == "3"){
			res = findByKeyword(detail);
		}else if(cmd == "4"){
			res = findByPublisher(detail);
		}else if(cmd == "5"){
			res = findByYear(detail);
		}
		if(res.size() == 0){
			cout << "Not Found" << endl;
			continue;
		}
		sort(res.begin(),res.end(),cmp);
		for(int i=0; i<res.size(); i++){
			cout <<	res[i].id << endl;
		}
	}
	return 0;
}